<?php
/**
 * Created by PhpStorm.
 * User: taotao
 * Date: 2019/9/24
 * Time: 15:25
 */

namespace App\Http\Controllers\WebApi\UserCenter;


use App\Http\Controllers\WebApi\BaseController;
use App\Http\Service\OrdersService;
use App\Models\Admin\OrderTempModel;
use App\Models\CompanysModel;
use App\Models\OrderCommentModel;
use App\Repositories\Admin\OrderBaleRepository;
use Illuminate\Http\Request;

class OrderController extends BaseController
{

    protected $service;

    public function __construct()
    {
        parent::__construct();
        $this->service = new OrdersService();
    }

    /**
     * 发布的订单
     * @param Request $request
     * @return array
     */
    public function pushingLists(Request $request)
    {
        $userType = $request->get('loginType');
        if($userType == 1){
            //return $this->error(30005);
        }
        $uid = $request->get('loginUid');
        $page = $request->input('page',1);
        $pageSize = $request->input('pagesize',20);

        $myOrders = $this->service->pushingLists($uid,$page,$pageSize);

        return $this->success($myOrders);

    }

    /**
     * 抢单中的订单
     * @param Request $request
     * @return array
     */
    public function baleLists(Request $request)
    {
        $userType = $request->get('loginType');
        if($userType == 1){
            //return $this->error(30005);
        }
        $uid = $request->get('loginUid');
        $page = $request->input('page',1);
        $pageSize = $request->input('pagesize',20);

        $myOrders = $this->service->baleLists($uid,$page,$pageSize);

        return $this->success($myOrders);

    }

    /**
     * 生产中的订单
     * @param Request $request
     * @return array
     */
    public function produceLists(Request $request)
    {
        $userType = $request->get('loginType');
        if($userType == 1){
            //return $this->error(30005);
        }
        $uid = $request->get('loginUid');
        $page = $request->input('page',1);
        $pageSize = $request->input('pagesize',20);

        $myOrders = $this->service->produceLists($uid,$page,$pageSize);

        return $this->success($myOrders);

    }

    /**
     * 已完成的订单
     * @param Request $request
     * @return array
     */
    public function overLists(Request $request)
    {
        $userType = $request->get('loginType');
        if($userType == 1){
            //return $this->error(30005);
        }
        $uid = $request->get('loginUid');
        $page = $request->input('page',1);
        $pageSize = $request->input('pagesize',20);

        $myOrders = $this->service->overLists($uid,$page,$pageSize);

        return $this->success($myOrders);

    }

    /**
     * 评价订单
     * @param Request $request
     * @return array
     */
    public function commentOrder(Request $request)
    {
        $userType = $request->get('loginType');
        if($userType == 1){
            //return $this->error(30005);
        }

        $uid = intval($request->input('loginUid',0));
        $id = intval($request->input('id',0));
        if(!$id){
            return $this->error(30002);
        }
        $content = trim($request->input('content',0));
        $level = intval($request->input('level',0));    //0一般 1良好 2很好 3差 4很差

        $orderComment = OrderTempModel::where('oid',$id)->where('uid',$uid)->count();
        if($orderComment){
            return $this->success([]);
        }

        $insertData = [
            'oid'=>$id,
            'uid'=>$uid,
            'content'=>$content,
            'level'=>$level,
            'addtime'=>time()
        ];

        if(OrderCommentModel::insert($insertData)){
            return $this->success([]);
        }else{
            return $this->error(50001);
        }
    }

    /**
     * 抢单详情
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function grapDetail(Request $request)
    {
        $userType = $request->get('loginType');
        if($userType == 1){
            //return $this->error(30005);
        }
        $uid = intval($request->get('loginUid'));
        $id = intval($request->input('id',0));
        if(!$id){
            return $this->toJSON(-2);
        }
        $dataArr = [];

        $OrderBaleRepository = new OrderBaleRepository();
        $result = $OrderBaleRepository->getCategoryOrdersDetail($id);
        if(!$result){
            return $this->toJSON(-3);
        }

        $dataArr['name'] = $result['name'];
        $dataArr['cate'] = $result['cate'];  //货单分类
        $dataArr['styleNo'] = $result['styleNo']; //款号
        $dataArr['styleName'] = $result['styleName']; //款式名称
        $dataArr['scence'] = $result['scence']; //使用场景
        $dataArr['material'] = $result['material']; //材质
        $dataArr['tech'] = $result['tech']; // 工艺要求
        $dataArr['num'] = $result['num'];
        $dataArr['money'] = $result['money'];
        $dataArr['allmoney'] = $result['money'] * $result['num'];
        $dataArr['proType'] = $result['proType'];
        $dataArr['region'] = $result['region'];
        $dataArr['proWay'] = $result['proWay'];
        $dataArr['period'] = $result['period'];
        $dataArr['detail'] = $result['detail']; //货单描述
        $dataArr['sizelist'] = $result['sizelist'] ? json_decode($result['sizelist']) : []; //尺码
        $dataArr['orderType'] = $result['orderType']; //类型
        $dataArr['design'] = $result['design']; //款式
        $dataArr['img'] = $result['img'];
        $dataArr['imgDetails'] = $result['imgDetails'] ? json_decode($result['imgDetails']) : [];

        //抢单工厂信息
        $factory = CompanysModel::where('uid',$uid)->first(['com_name','com_tel','com_address']);
        $dataArr['factory'] = $factory;

        return $this->success($dataArr);
    }

}